package com.xln.furns.dao.impl;

import com.xln.furns.dao.OrderItemDAO;
import com.xln.furns.entity.OrderItem;

import java.util.List;

public class OrderItemDAOimpl extends BasicDAO implements OrderItemDAO {
    @Override
    public int saveOrderItem(OrderItem orderItem) {
        String sql = "insert into `order_item`(`id`,`name`,`price`,`count`,`total_price`,`order_id`)\n" +
                "VALUES(?,?,?,?,?,?)";

        return update(sql,orderItem.getId(),orderItem.getName(),orderItem.getPrice(),orderItem.getCount(),orderItem.getTotalPrice(),orderItem.getOrderId());
    }

    @Override
    public List<OrderItem> queryOrderItem() {
        String sql = "select `id`,`name`,`price`,`count`,`total_price` as `totalPrice`,`order_id` as `orderId` from `order_item`";

        return queryMulti(sql,OrderItem.class);
    }

    @Override
    public int getTotalRowOrderItem(String orderId) {
        String sql = "SELECT COUNT(*) FROM `order_item` where `order_id`=?";

        return ((Number) queryScalar(sql,orderId)).intValue();
    }

    @Override
    public List<OrderItem> getPageItemPage(String orderId, Integer begin, Integer pageSize) {
        String sql = "SELECT `id`,`name`,`price`,`count`,`total_price` AS `totalPrice`,`order_id` AS `orderId` FROM `order_item` \n" +
                " WHERE `order_id`=? ORDER BY `order_id` DESC LIMIT ?,?";
        List<OrderItem> list = queryMulti(sql,OrderItem.class,orderId,begin,pageSize);
        return list;
    }
    @Override
    public List<OrderItem> getPageItem(String orderId) {
        String sql = "SELECT `id`,`name`,`price`,`count`,`total_price` AS `totalPrice`,`order_id` AS `orderId` FROM `order_item` \n" +
                " WHERE `order_id`=? ORDER BY `order_id` DESC";
        List<OrderItem> list = queryMulti(sql,OrderItem.class,orderId);
        return list;
    }
}
